<template>
  <div class="app-container">
    <div class="module-title">
      <svg-icon icon-class="chart" style="font-size:28px;margin-right:8px;"/>
      <span>成本核算</span>
      <span class="module-desc">管理生产工单成本核算明细</span>
    </div>
    <div class="module-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="工单号" prop="workOrder">
          <el-input
            v-model="queryParams.workOrder"
            placeholder="请输入工单号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['calculation:calculation:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['calculation:calculation:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['calculation:calculation:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['calculation:calculation:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="calculationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="工单号" align="center" prop="workOrder" />
        <el-table-column label="工单数量" align="center" prop="orderQuantity">
          <template slot-scope="scope">
            <span class="quantity-amount">{{ scope.row.orderQuantity || '0' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="物料名称" align="center" prop="materialName" />
        <el-table-column label="物料规格" align="center" prop="specification" />
        <el-table-column label="物料成本" align="center" prop="materialCost">
          <template slot-scope="scope">
            <span class="cost-amount">¥{{ scope.row.materialCost || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="加工费用" align="center" prop="processingFee">
          <template slot-scope="scope">
            <span class="cost-amount">¥{{ scope.row.processingFee || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="其他费用" align="center" prop="otherFees">
          <template slot-scope="scope">
            <span class="cost-amount">¥{{ scope.row.otherFees || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="总成本" align="center" prop="totalCost">
          <template slot-scope="scope">
            <span class="total-cost">¥{{ scope.row.totalCost || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="工具" align="center" prop="tool" />
        <el-table-column label="工资" align="center" prop="salary">
          <template slot-scope="scope">
            <span class="cost-amount">¥{{ scope.row.salary || '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['calculation:calculation:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['calculation:calculation:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改成本核算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="工单号" prop="workOrder">
              <el-input v-model="form.workOrder" placeholder="请输入工单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单数量" prop="orderQuantity">
              <el-input v-model="form.orderQuantity" placeholder="请输入工单数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料规格" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入物料规格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物料成本" prop="materialCost">
              <el-input v-model="form.materialCost" placeholder="请输入物料成本">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加工费用" prop="processingFee">
              <el-input v-model="form.processingFee" placeholder="请输入加工费用">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="其他费用" prop="otherFees">
              <el-input v-model="form.otherFees" placeholder="请输入其他费用">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总成本" prop="totalCost">
              <el-input v-model="form.totalCost" placeholder="自动计算" disabled>
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="工具" prop="tool">
              <el-input v-model="form.tool" placeholder="请输入工具" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工资" prop="salary">
              <el-input v-model="form.salary" placeholder="请输入工资">
                <template slot="prepend">¥</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCalculation, getCalculation, delCalculation, addCalculation, updateCalculation } from "@/api/calculation/calculation"

export default {
  activated() {
    this.getList()
  },
  name: "Calculation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成本核算表格数据
      calculationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrder: null,
        materialName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询成本核算列表 */
    getList() {
      this.loading = true
      listCalculation(this.queryParams).then(response => {
        this.calculationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        workOrder: null,
        orderQuantity: null,
        materialName: null,
        specification: null,
        materialCost: null,
        processingFee: null,
        otherFees: null,
        totalCost: null,
        tool: null,
        salary: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workOrder)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/calculation/calculation/add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const workOrder = row.workOrder || this.ids
      getCalculation(workOrder).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改成本核算"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.workOrder != null) {
            updateCalculation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCalculation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workOrders = row.workOrder || this.ids
      this.$modal.confirm('是否确认删除成本核算编号为"' + workOrders + '"的数据项？').then(function() {
        return delCalculation(workOrders)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('calculation/calculation/export', {
        ...this.queryParams
      }, `calculation_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/module-card.scss";

.module-title {
  display: flex;
  align-items: center;
  font-size: 22px;
  font-weight: 700;
  color: #2980b9;
  margin-bottom: 18px;
  .module-desc {
    font-size: 14px;
    color: #888;
    font-weight: 400;
    margin-left: 18px;
  }
}

.cost-amount {
  color: #666;
  font-weight: 500;
}

.total-cost {
  color: #e74c3c;
  font-weight: 700;
  font-size: 14px;
}

.quantity-amount {
  color: #3498db;
  font-weight: 600;
  font-size: 14px;
}

.el-table {
  .el-table__body tr:hover > td {
    background: #eaf6ff !important;
  }
}

.el-dialog {
  .el-form-item {
    margin-bottom: 20px;
  }

  .el-input-group__prepend {
    background: #f0f4fa;
    color: #2980b9;
    font-weight: 600;
    border-color: #dbeafe;
  }
}
</style>
